home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / Argus Frameworks 2.1 / Argus Libraries 2.1 / FnIO.cp < prev    next >
Text File  |  1996-01-02  |  16KB  |  598 lines

  1. /**********************************************************************
  2.  
  3.     FnIO.cp
  4.  
  5. ***********************************************************************/
  6.  
  7. /*
  8.     These functions allow you to read, write, and print text files in
  9.     the form of a TE Record.
  10.     
  11.     Functions Include:
  12.     
  13.       FnAE_OpenTextFile       Puts file into TE Record
  14.       FnIO_SaveTextFile       Save current file
  15.       FnIO_SaveAsTextFile     You get a chance to rename file
  16.       FnIO_PageSetup          Change default print setup
  17.       FnIO_PrintTERecord      Print contents of TE Record
  18.       
  19.       // Semi-private
  20.       FnIO_CreateFile
  21.       FnIO_ReadFile
  22.       FnIO_WriteFile
  23.       FnIO_pStrCopy
  24.       FnIO_PrintText
  25.       FnIO_PrintDoc
  26.       FnIO_DrawText
  27. */
  28.  
  29. #include <Printing.h>
  30.  
  31. #define TAB_CHAR      ((char)'\t')
  32. #define WATCH         4
  33. #define MAX_CHAR      32000
  34.  
  35. // Prototypes
  36.  
  37. int  FnIO_OpenTextFile  ( TEHandle *te,         // Follow with a call
  38.                           /* results */         // to the function
  39.                           Str255   fileName,    // FnTE_DetSBarIncr
  40.                           short    *vRef,
  41.                           short    *refNum );
  42. int  FnIO_SaveTextFile  ( TEHandle *te,
  43.                           Str255   fileName,
  44.                           short    *vRef,
  45.                           short    *refNum );
  46. int  FnIO_SaveAsTextFile( TEHandle *te,
  47.                           Str255   fileName,
  48.                           /* results */
  49.                           short    *vRef,
  50.                           short    *refNum );
  51. void FnIO_PageSetup     ( THPrint *pPrintH );
  52. void FnIO_PrintTERecord ( TEHandle *te, THPrint *pPrintH );
  53.                       
  54. int  FnIO_CreateFile( Str255    fn,
  55.                       short     *vRef,
  56.                       short     *theRef );
  57. int  FnIO_ReadFile  ( short     refNum,
  58.                       TEHandle  *te );
  59. int  FnIO_WriteFile ( short     refNum,
  60.                       char      *p,
  61.                       long      num );
  62. void FnIO_pStrCopy  ( StringPtr p1,
  63.                       StringPtr p2 );
  64. void FnIO_PrintText ( THPrint *pPrintH,
  65.                       char    **hText,
  66.                       long    length,
  67.                       int     topMargin,
  68.                       int     bottomMargin,
  69.                       int     leftMargin,
  70.                       int     font,
  71.                       int     size,
  72.                       int     tabPixels);
  73. void FnIO_PrintDoc  ( THPrint *pPrintH,
  74.                       char    **hText,
  75.                       long    count,
  76.                       int     topMargin,
  77.                       int     bottomMargin,
  78.                       int     leftMargin,
  79.                       int     font,
  80.                       int     size,
  81.                       int     tabPixels );
  82. void FnIO_DrawText  ( char    *p, 
  83.                       int     count,
  84.                       int     topMargin,
  85.                       int     bottomMargin,
  86.                       int     leftMargin,
  87.                       int     tabPixels );
  88.  
  89. extern void FnErr_DisplayStr(   Str255 s1,
  90.                          Str255 s2,
  91.                          Str255 s3,
  92.                          Str255 s4,
  93.                          int    quitFlag );
  94.  
  95.  
  96. /********** OpenTextFile */
  97.  
  98. int FnIO_OpenTextFile( TEHandle *te,
  99.                        /* results */
  100.                        Str255   fileName,
  101.                        short    *vRef,
  102.                        short    *refNum )
  103. /*
  104.     Takes TE handle and fills it in with text from a file that the user
  105.     selects.  Modifies fileName, vRef, and refNum based on user input
  106.     and system calls.  Uses FnIO_pStrCopy, FnIO_CreateFile,
  107.     FnErr_DisplayStr, and FnIO_ReadFile functions.
  108. */
  109. {
  110.     int        ok;
  111.     Point      SFGwhere = { 90, 82 };
  112.     SFReply    reply;
  113.     SFTypeList fileTypes;
  114.     Str255     fn;
  115.     int        volRef;
  116.     short      referenceNum;
  117.     int        dialogHeight = 136;
  118.     int        dialogWidth = 348;
  119.     
  120.     ok = true;
  121.     fileTypes[0] = 'TEXT';
  122.     SFGwhere.v = (qd.screenBits.bounds.bottom/3) - (dialogHeight/2);
  123.     SFGwhere.h = (qd.screenBits.bounds.right/2) - (dialogWidth/2);
  124.     SFGetFile( SFGwhere, "\p", 0L, 1, fileTypes, 0L, &reply );
  125.     if( reply.good )
  126.     {
  127.         FnIO_pStrCopy( reply.fName, fn );
  128.         volRef = reply.vRefNum;
  129.         if( FSOpen( fn, volRef, &referenceNum ) != noErr )
  130.         {
  131.             FnErr_DisplayStr( "\pError opening file ", fileName,
  132.                 "\p", "\p", false );
  133.             ok = false;
  134.         }
  135.         else
  136.         {
  137.             FnIO_ReadFile( referenceNum, te );
  138.             FnIO_pStrCopy( fn, fileName );
  139.             *vRef = volRef;
  140.             *refNum = referenceNum;
  141.             FSClose( referenceNum );
  142.             
  143.             /*
  144.                Should do the following:
  145.                FnTE_DetSBarIncr
  146.                SetWTitle( window, fileName )
  147.                dirty = 0
  148.             */
  149.         }
  150.     }
  151.     else
  152.         ok = FALSE;
  153.  
  154.     return( ok );
  155. }
  156.  
  157.  
  158. /********** SaveTextFile */
  159.  
  160. int FnIO_SaveTextFile( TEHandle *te,
  161.                        Str255   fileName,
  162.                        short    *vRef,
  163.                        short    *refNum )
  164. /*
  165.     Takes TE handle and saves it out to existing file.  Uses
  166.     FnErr_DisplayStr, and FnIO_WriteFile functions.
  167. */
  168. {
  169.     int     ok;
  170.     SFReply reply;
  171.     
  172.     ok = TRUE;
  173.     if( FSOpen( fileName, *vRef, refNum ) != noErr )
  174.     {
  175.         FnErr_DisplayStr( "\pError opening file ", fileName,
  176.             "\p", "\p", FALSE );
  177.         ok = FALSE;
  178.     }
  179.     else
  180.     {
  181.         FnIO_WriteFile(
  182.             *refNum,
  183.             (*(***te).hText),
  184.             (long)(***te).teLength );
  185.         FSClose( *refNum );
  186.         
  187.         /*
  188.            Should do the following:
  189.            dirty = 0
  190.         */
  191.     }
  192.  
  193.     return( ok );
  194. }
  195.  
  196.  
  197. /********** SaveAsTextFile */
  198.  
  199. int FnIO_SaveAsTextFile( TEHandle *te,
  200.                          Str255   fileName,
  201.                          /* results */
  202.                          short    *vRef,
  203.                          short    *refNum )
  204. /*
  205.     Takes TE handle and initial guess of fileName, and saves it out
  206.     to a text file.  Modifies fileName, vRef, and refNum based on user
  207.     input and system calls.  Uses FnIO_pStrCopy, FnIO_CreateFile,
  208.     FnErr_DisplayStr, and FnIO_WriteFile functions.
  209. */
  210. {
  211.     int     ok;
  212.     Point   SFPwhere = { 106, 104 };
  213.     SFReply reply;
  214.     int     dialogHeight = 104;
  215.     int     dialogWidth = 304;
  216.     
  217.     ok = TRUE;
  218.     SFPwhere.v = (qd.screenBits.bounds.bottom/3) - (dialogHeight/2);
  219.     SFPwhere.h = (qd.screenBits.bounds.right/2) - (dialogWidth/2);
  220.     SFPutFile( SFPwhere, "\pSave file as", fileName, 0L, &reply );
  221.     if( reply.good )
  222.     {
  223.         FnIO_pStrCopy( reply.fName, fileName );
  224.         *vRef = reply.vRefNum;
  225.         if( !FnIO_CreateFile( fileName, vRef, refNum ) )
  226.         {
  227.             FnErr_DisplayStr( "\pError creating file ", fileName,
  228.                 "\p", "\p", FALSE );
  229.             ok = FALSE;
  230.         }
  231.         else
  232.         {
  233.             FnIO_WriteFile(
  234.                 *refNum,
  235.                 (*(***te).hText),
  236.                 (long)(***te).teLength );
  237.             FSClose( *refNum );
  238.  
  239.             /*
  240.                Should do the following:
  241.                SetWTitle( window, fileName )
  242.                dirty = 0
  243.             */
  244.         }
  245.     }
  246.     else
  247.         ok = FALSE;
  248.  
  249.     return( ok );
  250. }
  251.  
  252.  
  253. /********** PageSetup */
  254.  
  255. void FnIO_PageSetup( THPrint *pPrintH )
  256. /*
  257.     Takes a pointer to a THPrint handle, initialized to NULL, and
  258.     uses it to display standard page setup dialog.  First time 
  259.     through, the Record is initialized to point to a default set of
  260.     data.
  261. */
  262. {
  263.     PrOpen();
  264.     if( *pPrintH == NULL )
  265.     {
  266.         *pPrintH = (TPrint **)NewHandle( sizeof( TPrint ) );
  267.         PrintDefault( *pPrintH );
  268.     }
  269.     PrStlDialog( *pPrintH );
  270.     PrClose();
  271. }
  272.  
  273.  
  274. /********** PrintTERecord */
  275.  
  276. void FnIO_PrintTERecord( TEHandle *te, THPrint *pPrintH )
  277. /*
  278.     Takes pointers to a TE Handle and Print Record, and prints text
  279.     using default font, size, and margins.
  280. */
  281. {
  282.     int topMargin    = 36;
  283.     int bottomMargin = 36;
  284.     int leftMargin   = 54;
  285.     int font         = monaco;
  286.     int size         = 10;
  287.     int tabChars     = 4;
  288.     
  289.     int tabPixels;
  290.     long teLength;
  291.     
  292.     tabPixels = StringWidth("\pmmmm");
  293.     teLength = (long)(***te).teLength;
  294.     FnIO_PrintText( pPrintH,
  295.                     (***te).hText,
  296.                     teLength,
  297.                     topMargin,
  298.                     bottomMargin,
  299.                     leftMargin,
  300.                     font,
  301.                     size,
  302.                     tabPixels );
  303. }
  304.  
  305.  
  306. /********** CreateFile */
  307.  
  308. int  FnIO_CreateFile( Str255    fn,
  309.                       short     *vRef,
  310.                       short     *theRef )
  311. /*
  312.     Creates a file.
  313. */
  314. {
  315.     OSErr io;
  316.     OSType creator;
  317.     OSType fileType;
  318.     char *c;
  319.     
  320.     c = (char *)&creator;
  321.     c[0] = '?';
  322.     c[1] = '?';
  323.     c[2] = '?';
  324.     c[3] = '?';
  325.  
  326.     c = (char *)&fileType;
  327.     c[0] = 'T';
  328.     c[1] = 'E';
  329.     c[2] = 'X';
  330.     c[3] = 'T';
  331.  
  332.     io = Create( fn, *vRef, creator, fileType );
  333.     if( (io == noErr) || (io == dupFNErr) )
  334.         io = FSOpen( fn, *vRef, theRef );
  335.         
  336.     return( (io == noErr) || (io == dupFNErr) );
  337. }
  338.  
  339.  
  340. /********** ReadFile */
  341.  
  342. int FnIO_ReadFile( short refNum, TEHandle *te )
  343. /*
  344.     Reads text file into referenced TE handle.
  345. */
  346. {
  347.     char buffer[256];
  348.     long count;
  349.     int  io;
  350.     
  351.     TESetSelect( 0, (***te).teLength, *te );
  352.     TEDelete( *te );
  353.     do
  354.     {
  355.         count = 256;
  356.         if( ((***te).teLength + count) >= MAX_CHAR )
  357.         {
  358.             FnErr_DisplayStr(   
  359.                 "\pFile too large to read, ",
  360.                 "\pit has been truncated to fit ",
  361.                 "\psize limitations.",
  362.                 "\p",
  363.                 false );  // don't quit
  364.             io = eofErr;
  365.         }
  366.         else
  367.         {
  368.             io = FSRead( refNum, &count, &buffer );
  369.             TEInsert( &buffer, count, *te );
  370.         }
  371.     } while( io == noErr );
  372.     
  373.     return( io == eofErr );
  374. }
  375.  
  376.  
  377. /********** WriteFile */
  378.  
  379. int  FnIO_WriteFile ( short     refNum,
  380.                       char      *p,
  381.                       long      num )
  382. /*
  383.     Writes text to a file.
  384. */
  385. {
  386.     int io;
  387.     
  388.     io = FSWrite( refNum, &num, p );
  389.     if( io == noErr )
  390.         io = SetEOF( refNum, num );
  391.     return( io );
  392. }
  393.  
  394.  
  395. /********** pStrCopy */
  396.  
  397. void FnIO_pStrCopy  ( StringPtr p1,
  398.                       StringPtr p2 )
  399. /*
  400.     Copies Pascal string from p1 to p2.
  401. */
  402. {
  403.     register int len;
  404.     
  405.     len = *p2++ = *p1++;
  406.     while( --len >= 0 ) *p2++ = *p1++;
  407. }
  408.  
  409.  
  410. /********** PrintText */
  411.  
  412. void FnIO_PrintText( THPrint *pPrintH,
  413.                      char    **hText,
  414.                      long    length,
  415.                      int     topMargin,
  416.                      int     bottomMargin,
  417.                      int     leftMargin,
  418.                      int     font,
  419.                      int     size,
  420.                      int     tabPixels)
  421. /*
  422.     Prints a TE record.
  423. */
  424. {
  425.     TPPrPort  printPort;
  426.     GrafPtr   oldPort;
  427.     TPrStatus prStatus;
  428.     int       copies;
  429.     
  430.     int        howMany;
  431.     CursHandle hCurs;
  432.     Cursor     waitCursor;
  433.     
  434.     PrOpen();
  435.     if( *pPrintH == NULL )
  436.     {
  437.         *pPrintH = (TPrint **)NewHandle( sizeof( TPrint ) );
  438.         PrintDefault( *pPrintH );
  439.     }
  440.     if( tabPixels <= 0 ) tabPixels = StringWidth("\pm");
  441.     SetCursor( &qd.arrow );
  442.     if( PrJobDialog( *pPrintH ) != 0 )
  443.     {
  444.         hCurs = GetCursor( WATCH );
  445.         waitCursor = **hCurs;
  446.         SetCursor( &waitCursor );
  447.         GetPort( &oldPort );      
  448.         if( (***pPrintH).prJob.bJDocLoop == bDraftLoop )
  449.             howMany = (***pPrintH).prJob.iCopies;
  450.         else
  451.             howMany = 1;        
  452.         for( copies = howMany; copies > 0; copies-- )
  453.         {
  454.             FnIO_PrintDoc( pPrintH,
  455.                            hText,
  456.                            length,
  457.                            topMargin,
  458.                            bottomMargin,
  459.                            leftMargin,
  460.                            font,
  461.                            size,
  462.                            tabPixels );
  463.             if((***pPrintH).prJob.bJDocLoop == bSpoolLoop &&
  464.                 PrError() == noErr)
  465.                 PrPicFile( *pPrintH, 0L, 0L, 0L, &prStatus );
  466.         }
  467.         SetPort( oldPort );
  468.     }
  469.     PrClose();
  470.     SetCursor( &qd.arrow );
  471. }
  472.  
  473.  
  474. /********** PrintDoc */
  475.  
  476. void FnIO_PrintDoc ( THPrint *pPrintH,
  477.                      char    **hText,
  478.                      long    count,
  479.                      int     topMargin,
  480.                      int     bottomMargin,
  481.                      int     leftMargin,
  482.                      int     font,
  483.                      int     size,
  484.                      int     tabPixels )
  485. /*
  486.     Function called by FnIO_PrintText.  Calls function FnIO_DrawText.
  487. */
  488. {
  489.     register int  line = 0;
  490.     register int  lastLineOnPage = 0;
  491.     int           length;
  492.     Rect          printRect;
  493.     int           linesPerPage;
  494.     int           lineBase;
  495.     int           lineHeight;
  496.     register char *ptr, *p1;
  497.     FontInfo      info;
  498.     TPPrPort      printPort;
  499.     
  500.     int           rightMargin = 36;  // don't go off page!
  501.     int           max_length;
  502.     
  503.     printPort = PrOpenDoc( *pPrintH, 0L, 0L );
  504.     SetPort( (GrafPtr)printPort );
  505.     TextFont( font );
  506.     TextSize( size );
  507.     printRect = (***pPrintH).prInfo.rPage;
  508.     GetFontInfo( &info );
  509.     lineHeight = info.leading + info.ascent + info.descent;
  510.     linesPerPage = 
  511.         (printRect.bottom - printRect.top - topMargin - bottomMargin)
  512.         / lineHeight;
  513.  
  514.     // determine max characters per line (assumes proportional font!)
  515.     max_length = ( (printRect.right - printRect.left) -
  516.                  (leftMargin + rightMargin) ) / (info.widMax-2);
  517.  
  518.     HLock( hText );
  519.     ptr = p1 = (*hText);
  520.     do
  521.     {
  522.         PrOpenPage( printPort, 0L );
  523.         lastLineOnPage += linesPerPage;
  524.         MoveTo( printRect.left + leftMargin, 
  525.             (lineBase = printRect.top + lineHeight + topMargin) );
  526.         do
  527.         {   // print line
  528.             while((ptr <= (*hText)+count) && (*ptr++ != (char)'\r')) {};
  529.             if((length = (int)(ptr-p1) - 1) > 0)
  530.             {
  531.                 if( length > max_length )          // wrap-around text
  532.                 {
  533.                     length = max_length;
  534.                     ptr = (char*)(p1 + length);
  535.                     while( *ptr != (char)' ' )
  536.                     {
  537.                         *ptr--;
  538.                         length--;
  539.                     }
  540.                     *ptr++;
  541.                     length++;
  542.                     if( length <= 0 )
  543.                     {
  544.                         length = max_length;
  545.                         ptr = (char*)(p1 + length);
  546.                     }
  547.                 }
  548.                 FnIO_DrawText( p1,
  549.                                length,
  550.                                topMargin,
  551.                                bottomMargin,
  552.                                leftMargin,
  553.                                tabPixels );
  554.             }
  555.             MoveTo( printRect.left + leftMargin,
  556.                 (lineBase += lineHeight) );
  557.             p1 = ptr;
  558.         } while((++line != lastLineOnPage) && (ptr < (*hText) + count));
  559.         PrClosePage( printPort );
  560.     } while( ptr < (*hText) + count );
  561.     HUnlock( hText );
  562.     PrCloseDoc( printPort );
  563. }
  564.  
  565.  
  566. /********** DrawText */
  567.  
  568. void FnIO_DrawText ( char    *p, 
  569.                      int     count,
  570.                      int     topMargin,
  571.                      int     bottomMargin,
  572.                      int     leftMargin,
  573.                      int     tabPixels )
  574. /*
  575.     Function called by FnIO_PrintDoc.
  576. */
  577. {
  578.     register char *p1, *p2;
  579.     int           len;
  580.     Point         pt;
  581.     
  582.     p1 = p;
  583.     p2 = p + count;
  584.     while( p < p2 )
  585.     {
  586.         while( (p1 < p2) && (*p1 != TAB_CHAR) ) *p1++;
  587.         if( (len = p1 - p) > 0 ) DrawText( p, 0, (p1-p) );
  588.         if( *p1 == TAB_CHAR )
  589.         {
  590.             GetPen( &pt );
  591.             Move( (tabPixels - (pt.h - leftMargin)%tabPixels), 0 );
  592.             *p1++;
  593.         }
  594.         p = p1;
  595.     }
  596. }
  597.  
  598. // End of File